<template>
    <div class="box bline">
        <div class="floatBtn">
            <el-button icon="el-icon-refresh" size="mini" type="text" @click="getMoneyLx"></el-button>
        </div>
        <div class="title">资金流向(主/超)</div>
        <div class="list moneyList" ref="moneyEl">
            <el-row v-for="(item,idx) in moneyLxList" :key="idx" class="mt2">
                <span class="colspan">{{item.col1}}</span>
                <span class="colspan" :class="item.col2color">{{item.col2}}</span>
                <span class="colspan" :class="item.col3color">{{item.col3}}</span>
            </el-row>
        </div>
    </div>
</template>

<script>

export default {
    data() {
        return {
            moneyls:"http://push2.eastmoney.com/api/qt/stock/fflow/kline/get?lmt=0&klt=1&secid=1.000001&secid2=0.399001&fields1=f1,f2,f3,f7&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61,f62,f63&ut=b2884a393a59ad64002292a3e90d46a5&cb=&",
            moneyLxList:[],//资金流向数据
            timer: null
        }
    },
    inject: ['cfg',"isQry","getSleepTime","isMoneyException","addException"],
    

    mounted() {
        this.startTimer();
    },

    methods: {
        startTimer(){
            this.getMoneyLx();
            this.$once('hook:beforeDestroy', () => {   
                clearTimeout(this.timer);
            })
            this.doTimer();
        },
        doTimer(){
            clearTimeout(this.timer);
            if(this.isQry()){
                this.getMoneyLx();
            }
            var sleep = this.getSleepTime();

            this.timer = setTimeout(()=>{
                this.doTimer();
            },sleep);
        },
        getMoneyLx(){
            this.$axios.get(this.moneyls).then((res) => {
                if(res.data && res.data.data && res.data.data.klines){
                    var datas = res.data.data.klines;
                    var list = [];
                    var preItem = null;
                    datas.map((item)=>{
                        var arr = item.split(",")
                        var item = {
                            col1: arr[0].substring(11),
                            col2: this.formatYiMoney(arr[1]),
                            col3: this.formatYiMoney(arr[5]),
                        }
                        // if(item.col1>"13:11")return;
                        if(preItem){
                            item.col2color=this.getColor(item.col2,preItem.col2)
                            item.col3color=this.getColor(item.col3,preItem.col3)

                            item.col2up = item.col2>preItem.col2;
                            item.col3up = item.col3>preItem.col3;
                        }
                        list.push(item);
                        preItem = item;
                    });
                    this.moneyLxList = list;
                    this.$nextTick(()=>{
                        this.$refs.moneyEl.scrollTop = this.$refs.moneyEl.scrollHeight;
                    });
                    if(this.exceptionTime){
                        var cur =  new Date().getTime();
                        if(cur-this.exceptionTime<30000) return;
                    };
                    if(!this.cfg.event.main_money) return;
                    //判断是否需要提示数据异常
                    if(list<5 || list[list.length-1].col1>="15:00") return;
                    if(this.isMoneyException(list,"col2up","主力")){
                        this.exceptionTime = new Date().getTime();
                        return;
                    }
                    if(this.isMoneyException(list,"col3up","超大")){
                        this.exceptionTime = new Date().getTime();
                        return;
                    }
                }else{
                    this.$message.error(JSON.stringify(res.data));
                }
            })

            
        },
    }
}
</script>


